import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class GuessNumber {
    public GuessNumber(){
        final TextField num;
        final int k=(int) (Math.random()*100+1);
        JFrame f=new JFrame();
        f.setVisible(true);
        f.setSize(400,250);
        f.setLocationRelativeTo(null);
        f.setTitle("猜数字游戏");

        Container con=f.getContentPane();
        con.setLayout(new GridLayout(3,1));

        JPanel pan1=new JPanel();
        JLabel title=new JLabel("猜数字游戏");
        title.setFont(new Font("宋体",Font.BOLD,20));
        pan1.add(title);
        con.add(pan1);

        JPanel pan2=new JPanel();
        JLabel name=new JLabel("请输入您猜的数字：");
        num=new TextField(10);
        pan2.add(name);pan2.add(num);
        con.add(pan2);

        JPanel pan3=new JPanel();
        JButton b_log=new JButton("确定");
        b_log.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int user_num;
                String guess=num.getText();
                user_num=Integer.parseInt(guess);
                if(user_num>k)
                    JOptionPane.showMessageDialog(null, "猜大了！");
                else
                if(user_num<k)
                    JOptionPane.showMessageDialog(null, "猜小了！");
                else
                    JOptionPane.showMessageDialog(null, "猜对了！");
            }
        });
        JButton b_exit=new JButton("取消");
        b_exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        pan3.add(b_log);pan3.add(b_exit);
        con.add(pan3);
    }

}
